package org.zhaozhen.org.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.zhaozhen.basic.service.IBaseService;
import org.zhaozhen.basic.service.impl.BaseServiceImpl;
import org.zhaozhen.basic.util.PageList;
import org.zhaozhen.org.domain.Department;
import org.zhaozhen.org.mapper.DepartmentMapper;
import org.zhaozhen.org.query.DepartmentQuery;
import org.zhaozhen.org.service.IDepartmentService;

import java.util.List;
@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public void add(Department department) {
        departmentMapper.save(department);//数据库三大金刚返回了自增主键
        this.update(department);
    }

    @Override
    public void update(Department department) {
        //1.新增顶级部门 /自己的id
        String dirPath = "";
        if(department.getParent() == null){
            dirPath = "/"+department.getId();
        }else{
            //2.新增非顶级部门
            // 父亲部门的dirPath/自己的id
            Department parent = departmentMapper.loadById(department.getParent().getId());
            dirPath = parent.getDirPath() +"/"+department.getId();
        }
        department.setDirPath(dirPath);
        departmentMapper.update(department);
    }


    @Override
    public List<Department> queryTree() {
        return  departmentMapper.queryTree();
    }
}
